package cn.qiao.benchmark;

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import org.apache.commons.pool2.impl.GenericObjectPool;

/**
 * desc:
 *
 * @author qiao
 * @date 2024年03月07日 22:34
 */
public class LettucePoolImpl implements RedisService {


    private  GenericObjectPool<StatefulRedisConnection<String, String>> pool;


    public LettucePoolImpl(GenericObjectPool pool) {
        this.pool = pool;
    }

    @Override
    public void set(String key, String value) {
        try(StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
            RedisCommands<String, String> sync = connection.sync();
            sync.set(key,value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String get(String key) {
        try(StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
            RedisCommands<String, String> sync = connection.sync();
            return sync.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
